<?php
/**
 * Created by PhpStorm.
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */

namespace App\Repositories;

use App\Models\ShopDish;
use App\Models\ShopDishAttribute;
use Illuminate\Support\Facades\DB;
use App\Models\Shop;
use App\Models\User;
use App\Models\ShopInfo;
use Illuminate\Support\Facades\Auth;

class ShopRepository
{
    //平台首页服务层
    public function Index($lng,$lat,$page,$num,$type,$business_code='')
    {
//        $num=1;
//        $page=3;
        //分页
        $Page =  customPage($page,$num);
//        dd($Page);
        //排序类型
        if($type == 'sales'){
            $type = 'i.sales desc';
        }else if($type == 'score'){
            $type = 'i.score desc';
        }else if($type == 'distance'){
            $type = 'distance asc';
        }
        try{
//            select ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-latitude*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(latitude*PI()/180)*POW(SIN(($longitude*PI()/180-longitude*PI()/180)/2),2)))*1000) AS distance
//            FROM shop having distance <= 5000 order by distance asc
            //商圈
            if($business_code!=''){
                $business_code = ' AND s.business_code='.$business_code;
                $Shop = DB::select('SELECT s.id,s.is_list,s.is_reserve,s.is_takeout,s.shop_address,s.shop_name,s.shop_phone,s.shop_pic,i.*,
                ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-
                latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.'*PI()/180-
                longitude*PI()/180)/2),2)))*1000)
                AS distance FROM shops s,shop_info i WHERE s.is_check=1'.$business_code.' AND s.id=i.shop_id AND s.deleted_at IS NULL AND i.deleted_at IS NULL ORDER BY '.$type.
                    ' limit '.$Page['begin'].','.$Page['end'].''
                );
            }else{
                $Shop = DB::select('SELECT s.id,s.is_list,s.is_reserve,s.is_takeout,s.shop_address,s.shop_name,s.shop_phone,s.shop_pic,i.*,
                ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-
                latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.'*PI()/180-
                longitude*PI()/180)/2),2)))*1000)
                AS distance FROM shops s,shop_info i WHERE s.is_check=1 AND s.id=i.shop_id AND s.deleted_at IS NULL AND i.deleted_at IS NULL ORDER BY '.$type.
                    ' limit '.$Page['begin'].','.$Page['end'].''
                );
            }
            return $Shop;
        }catch (\Exception $e){
//            dd($e->getMessage());
            return $Shop = [];
        }

        /*foreach($Shop as $k=>$v){
            $Shop[$k]->shopinfo = ShopInfo::find($v->id);
        }*/

    }

    
        //首页推荐
        public function getRecommendShop()
        {
            $Shop = DB::select(
                'SELECT id,is_list,is_reserve,is_takeout,shop_address,shop_name,shop_phone,shop_pic,note 
                           FROM shops WHERE is_check=1 AND 	recommend != 0 AND deleted_at is null'
            );
            foreach($Shop as $k=>$v){
                $Shop[$k]->shopinfo = ShopInfo::find($v->id);
            }
            return $Shop;
        }

        //商家首页服务层
        public function getShopIndex($id)
        {
            $Shop = Shop::where('is_check',1)->find($id);
            if($Shop!=NULL){
                $Shop['shopinfo'] = $Shop->shopinfo;
                $Shop['comment']  = $Shop->shopcomment;
                $Shop['room_desk'] = $Shop->shoproomtable;
                $Shop['shopdishes'] = $Shop->shopdishes;
                $Shop['coupon'] = $Shop->shopcoupon;
                //获取评论该商家的用户
                foreach($Shop->shopcomment as $k=>$v){
                    $comment_user = User::find($v['user_id']);
                    if($comment_user){
                        $Shop['comment'][$k]['user_name']= $comment_user->name;
                        $Shop['comment'][$k]['user_name']= $comment_user->avatar;
                    }else{
                        $Shop['comment'][$k]['user_name']= '';
                        $Shop['comment'][$k]['user_name']= '';
                    }
                }
                return $Shop;
            }else{
                return false;
            }

        }
    //商家首页服务层 小程序
    public function getShopIndexProgram($id)
    {
        $Shop = Shop::find($id);
        $res = $Shop;
        if($Shop!==NULL){
            $Shop['shopinfo'] = $Shop->shopinfo;

            $Shop['comment']  = $Shop->shopcomment;
            $Shop['room_desk'] = $Shop->shoproomtable;
//            $shop['shopdishes'] = $res->shopdishes;
            $shopdish = $Shop->shopdishes;

            $menu = $Shop->shopmenu;
            if ($shopdish->isNotEmpty()){
                $shopdish = $shopdish->toArray();
            }

            if ($menu->isNotEmpty()){
                $menu= $menu->toArray();
                for($i=0;$i<count($menu);$i++){
                    for($j=0;$j<count($shopdish);$j++){
                        if ($menu[$i]['id']==$shopdish[$j]['menu_id']){
                            $menu[$i]['dish'] = $this->getAllDishes($id,$menu[$i]['id']);
                            break;
                        }
                    }
                }
            }
            $Shop['designdish'] = $menu;
            //获取评论该商家的用户
            foreach($Shop->shopcomment as $k=>$v){
                $comment_user = User::find($v['user_id']);
                if($comment_user){
                    $Shop['comment'][$k]['user_name']= $comment_user->name;
                    $Shop['comment'][$k]['user_avatar']= $comment_user->avatar;
                }else{
                    $Shop['comment'][$k]['user_name']= '';
                    $Shop['comment'][$k]['user_avatar']= '';
                }
            }
        }
        return $Shop;
    }

    public function getAllDishes($shop_id,$menu_id){

        $dish = ShopDish::where('shop_id',$shop_id)->where('status',1)->where('menu_id',$menu_id)->get();
        $dish = $dish->toArray();

        for ($i=0;$i<count($dish);$i++){
            if (strpos($dish[$i]['thumb'],';')){
                $res = explode(';',$dish[$i]['thumb']);
                $dish[$i]['thumb']=$res[0];
            }
            if ($dish[$i]['is_attr'] == 1){
                $dish[$i]['attribute'] = ShopDishAttribute::where('dish_id',$dish[$i]['id'])->get();
                //处理多规格
                for($y=0;$y<count($dish[$i]['attribute']);$y++)
                {
                    $spec=explode(",", $dish[$i]['attribute'][$y]['spec']);
                    foreach($spec as $k2=>$v2)
                    {
                        $spec[$k2] = explode("|",$v2);
                        $specend[$k2]['spec_name'] = $spec[$k2][0];
                        $specend[$k2]['spec_price'] = $spec[$k2][1];
                    }
                    $dish[$i]['attribute'][$y]['spec']=$specend;
                }
            }
        }
        return $dish;

    }

    //预定首页
    public function getReserveShopIndex($lng,$lat,$page,$num,$type,$business_code='',$s='')
    {
        //分页
        $Page =  customPage($page,$num);
        //排序类型
        switch ($type)
        {
            case 'distance'://距离
                $type = 'distance asc';
                break;
            case 'sales'://销量
                $type = 'i.sales desc';
                break;
            case 'score'://评分
                $type = 'i.score desc';
                break;
            default:
                $type = 'distance asc';
        }
        //商圈
        if($business_code!=''){
            $business_code = ' AND s.business_code='.$business_code;
        }
        $search = '';
        if($s!=''){
            if (!is_string($s)){
                return show(20002,'请输入搜索词', '');
            }
            $search = ' AND s.shop_name like "%'.$s.'%"';
        }
        $Shop = DB::select('SELECT s.id,s.is_list,s.is_reserve,s.is_takeout,s.shop_address,s.shop_name,s.shop_phone,s.shop_pic,i.*,
                ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-
                latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.'*PI()/180-
                longitude*PI()/180)/2),2)))*1000)
                AS distance FROM shops s,shop_info i WHERE s.is_check=1'.$business_code.' AND s.is_reserve=1 AND s.id=i.shop_id AND s.deleted_at IS NULL AND i.deleted_at IS NULL'.$search.' ORDER BY '.$type.' limit '.$Page['begin'].','.$Page['end'].''
        );
        /*foreach($Shop as $k=>$v){
            $Shop[$k]->shopinfo = ShopInfo::find($v->id);
        }*/
        return $Shop;
    }

    //配送首页
    public function getTakeShopIndex($lng,$lat,$page,$num,$type,$type_id,$business_code='',$s='')
    {
        //分页
        $Page =  customPage($page,$num);
        //排序类型
        switch ($type)
        {
            case 'distance'://距离
                $type = 'distance asc';
                break;
            case 'sales'://销量
                $type = 'i.sales desc';
                break;
            case 'score'://评分
                $type = 'i.score desc';
                break;
            default:
                $type = 'distance asc';
        }
        //商圈
        if($business_code!=''){
            $business_code = ' AND s.business_code='.$business_code;
        }

        //分类
        if($type_id!=''){
            $type_id = " AND FIND_IN_SET($type_id,type_id)";
        }
        $search = '';
        if($s!=''){
            if (!is_string($s)){
                return show(20002,'请输入搜索词', '');
            }
            $search = ' AND s.shop_name like "%'.$s.'%"';
        }
        $Shop = DB::select('SELECT s.id,s.is_list,s.is_reserve,s.is_takeout,s.shop_address,s.shop_name,s.shop_phone,s.shop_pic,i.*,
                ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-
                latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.'*PI()/180-
                longitude*PI()/180)/2),2)))*1000)
                AS distance FROM shops s,shop_info i WHERE s.is_check=1'.$business_code.' AND s.is_takeout=1'.$type_id.' AND s.id=i.shop_id AND s.deleted_at IS NULL AND i.deleted_at IS NULL '.$search.' ORDER BY '.$type.' limit '.$Page['begin'].','.$Page['end'].''
        );

        return $Shop;
    }

    //排队首页
    public function getListShopIndex($lng,$lat,$page,$num,$type,$business_code='',$s='')
    {
        //分页
        $Page =  customPage($page,$num);
        //排序类型
        switch ($type)
        {
            case 'distance'://距离
                $type = 'distance asc';
                break;
            case 'sales'://销量
                $type = 'i.sales desc';
                break;
            case 'score'://评分
                $type = 'i.score desc';
                break;
            default:
                $type = 'distance asc';
        }
        //商圈
        if($business_code!=''){
            $business_code = ' AND s.business_code='.$business_code;
        }
        $search = '';
        if($s!=''){
            if (!is_string($s)){
                return show(20002,'请输入搜索词', '');
            }
            $search = ' AND s.shop_name like "%'.$s.'%"';
        }
        $Shop = DB::select('SELECT s.id,s.is_list,s.is_reserve,s.is_takeout,s.shop_address,s.shop_name,s.shop_phone,s.shop_pic,i.*,
                ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.'*PI()/180-
                latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.'*PI()/180-
                longitude*PI()/180)/2),2)))*1000)
                AS distance FROM shops s,shop_info i WHERE s.is_check=1'.$business_code.' AND s.is_takeout=1 AND s.id=i.shop_id AND s.deleted_at IS NULL AND i.deleted_at IS NULL '.$search.' ORDER BY '.$type.' limit '.$Page['begin'].','.$Page['end'].''
        );
        /*foreach($Shop as $k=>$v){
            $Shop[$k]->shopinfo = ShopInfo::find($v->id);
        }*/
        return $Shop;
    }


}
